package com.hs.jhzd.controller;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import com.hs.jhzd.repository.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hs.jhzd.entity.Disease;
import com.hs.jhzd.entity.DiseaseCategory;
import com.hs.jhzd.entity.Meeting;
import com.hs.jhzd.utils.WebResult;

@RequestMapping("/D")
@Controller
public class DoctorController {
    @Value("${jsbh}")
    private String jsbh;

    @Autowired
    private MeetingNativeRepository nativeRepository;

    @Autowired
    private DiseaseCategoryRepository diseaseCategoryRepository;

    @Autowired
    private DiseaseRepository diseaseRepository;
    @Autowired
    private ZfYyjlbRepository zfYyjlbRepository;

    public static final String DATE_FORMAT = "yyyyMMdd";
    public static final String DATE_FORMAT1 = "yyyy-MM-dd HH:mm:ss";

    /**
     * @param jsbh
     * @param rybh
     * @param name
     * @param startTime
     * @param endTime
     * @param type      检察官、所领导、管教、律师、医生
     * @param done      是否已处理
     * @return
     */
    @RequestMapping("/search")
    @ResponseBody
    public List<Meeting> search(String jsbh, String rybh, String name,
                                @DateTimeFormat(pattern = DATE_FORMAT1) Date startTime,
                                @DateTimeFormat(pattern = DATE_FORMAT1) Date endTime, String type, Boolean isDisease,
                                Boolean done) {
        return nativeRepository.search(jsbh, rybh, name, startTime, endTime, type, isDisease, done);
    }

    @RequestMapping("/admin/main")
    public String shiftMainPage(Model model, String jsbh, String rybh, String name,
                                @DateTimeFormat(pattern = DATE_FORMAT) Date startTime,
                                @DateTimeFormat(pattern = DATE_FORMAT) Date endTime, String type, Boolean isDisease,
                                Boolean done) {
        List<Meeting> meetings = search(jsbh, rybh, name, startTime, endTime, type, isDisease, done);
        model.addAttribute("jsbh", jsbh);
        model.addAttribute("results", meetings);
        return "doctor-list";
    }

    @RequestMapping("/diseaseCategory")
    @ResponseBody
    public List<DiseaseCategory> findDiseaseCategory() {
        return diseaseCategoryRepository.findAll();
    }
    @RequestMapping("/findDiseaseCategoryByid")
    @ResponseBody
    public DiseaseCategory findDiseaseCategoryByid(Long id) {
        Optional<DiseaseCategory> byId = diseaseCategoryRepository.findById(id);
        if(byId.isPresent()){
            return diseaseCategoryRepository.findById(id).get();
        }
        return null;
    }
    @RequestMapping("/saveDiseaseCategory")
    @ResponseBody
    public  boolean findDiseaseCategory(DiseaseCategory diseaseCategory) {
         diseaseCategoryRepository.save(diseaseCategory);
         return true;
    }
    @RequestMapping("/delDiseaseCategory")
    @ResponseBody
    public  boolean findDiseaseCategory(Long id) {
        diseaseCategoryRepository.deleteById(id);
        return true;
    }

    @RequestMapping("/diseases")
    @ResponseBody
    public List<Disease> findDiseases(String categoryCode) {
        if (StringUtils.isBlank(categoryCode)) {
            return null;
        }
        categoryCode = categoryCode.substring(0, 1);
        return diseaseRepository.findByCategoryCode(categoryCode);
    }

    /*
     * 预约的处理操作
     */
    @RequestMapping("/dispose")
    @ResponseBody
    public WebResult disposeAppointment(String rybh, @DateTimeFormat(pattern = DATE_FORMAT1) Date startTime, Integer  status) {
        Date endTime = new Date();
        int num = zfYyjlbRepository.updateDispose(rybh, startTime, status,endTime);
        if (num == 0) {
            return WebResult.FAIL("修改失败");
        } else {
            return WebResult.OK();
        }
    }

    @RequestMapping("/findDiseasesAll")
    @ResponseBody
    public List<Disease> findDiseases() {
        return diseaseRepository.findAll();
    }

    @RequestMapping("/findDiseasesById")
    @ResponseBody
    public Disease findDiseasesById(Long id) {
        Optional<Disease> byId = diseaseRepository.findById(id);
        if(byId.isPresent()){
            return byId.get();
        }
        return null;
    }

    @RequestMapping("/saveDiseases")
    @ResponseBody
    public boolean findDiseases(Disease disease) {
        diseaseRepository.save(disease);
        return true;
    }

    @RequestMapping("/delDiseases")
    @ResponseBody
    public boolean findDiseases(Long id) {
        diseaseRepository.deleteById(id);
        return true;
    }



    @RequestMapping("/admin/disease")
    public String diseasePage(Model model, String jsbh) {
      if (StringUtils.isBlank(jsbh) || StringUtils.equals(jsbh, "null")) {
        return "redirect:/D/admin/disease?jsbh=" + this.jsbh;
      }
        model.addAttribute("jsbh", jsbh);
        model.addAttribute("disease", diseaseRepository.findAll());
        return "disease-list";
    }

    @RequestMapping("/admin/diseaseCategory")
    public String diseaseCategoryPage(Model model, String jsbh) {
      if (StringUtils.isBlank(jsbh) || StringUtils.equals(jsbh, "null")) {
        return "redirect:/D/admin/diseaseCategory?jsbh=" + this.jsbh;
      }
        model.addAttribute("jsbh", jsbh);
        model.addAttribute("diseaseCategory", diseaseCategoryRepository.findAll());
        return "disease-type-list";
    }
}
